# Group Analysis over time

library(readstata13)
library(ggplot2)

b <- read.dta13("../source_data/bsa8320.dta")
b <- b[b$year>2009,]

years = c(2010:2020)

# people on bens don't really deserve help [higher = disagree more]
  b$sochelp[b$sochelp<1|b$sochelp>5] <-NA
  #b$sochelp <- scale(b$sochelp, center = TRUE, scale = TRUE)
  
  deserve=c()
  b1 <- b[!is.na(b$sochelp),]
  for(i in 1:length(years)){
    b1=subset(b,b$year==years[i])
    deserve[i]=(weighted.mean(b1$sochelp,b1$wtfactor,na.rm=T))
  }
  

# fiddling one way or another [higher = disagree more]
  b$dolefidl[b$dolefidl<1|b$dolefidl>5] <-NA
 # b$dolefidl <- scale(b$dolefidl, center = TRUE, scale = TRUE)
  
  fiddling=c()
  b1 <- b[!is.na(b$dolefidl),]
  for(i in 1:length(years)){
    b1=subset(b,b$year==years[i])
    fiddling[i]=(weighted.mean(b1$dolefidl,b1$wtfactor,na.rm=T))
  }



## plot
  deserve_data <- as.data.frame(cbind(
    c(deserve,fiddling),
    c(rep(c(2010:2020),2)),  #check
    c(rep("Most people on the dole are\nfiddling one way or another",11),
      rep("Most benefits recipients don't\ndeserve help",11))
  ))
  colnames(deserve_data) <- c("numbers","year","variable")
  deserve_data$numbers <- as.numeric(deserve_data$numbers)
  deserve_data$year <- as.numeric(deserve_data$year)
  
pdf(file="../generated_images/bsas_deservingness.pdf",height=6,width=8)
  ggplot(deserve_data, aes(x=year,linetype=variable)) +
    geom_line(aes(y=numbers)) +
    theme_bw() +
    scale_y_continuous(name="Mean Response (higher = disagree more)") +
    scale_x_continuous(name="",breaks=c(2010,2011,2012,2013,2014,2015,
                                        2016,2017,2018,2019,2020),
                               labels=c("2010","2011","2012","2013",
                                        "2014","2015","2016","2017",
                                        "2018","2019","2020")) +
    theme(legend.position="bottom",legend.title=element_blank()) +
    theme(text = element_text(size = 12)) 
dev.off()

  
